package com.atcuitest;

import android.util.Log;
import android.view.KeyEvent;

import com.android.uiautomator.core.UiObjectNotFoundException;
import com.sonymobile.uiautotestutils.ATCUiAutoTestCase;


public class CalculatorTest extends ATCUiAutoTestCase {

	private String TAG = "ATCAutoTest";
	private String appName="Calculator";
	private String testName="";
	private long start = 0;
	private long end = 0;
	public CalculatorTest(){
		super("Calculator");
	}
	public void setTestName(String test){
	    this.testName = test;
	}
	public String getTestName(){
	    return this.testName;
	}
	public void pressKeyString(String str, boolean s){
	    int [] key = {KeyEvent.KEYCODE_0,KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2,KeyEvent.KEYCODE_3,
	            KeyEvent.KEYCODE_4,KeyEvent.KEYCODE_5,KeyEvent.KEYCODE_6,
	            KeyEvent.KEYCODE_7,KeyEvent.KEYCODE_8,KeyEvent.KEYCODE_9};
	    if("0".equals(str)) pressKeyCode(key[0]);
	    if("1".equals(str)) pressKeyCode(key[1]);
	    if("2".equals(str)) pressKeyCode(key[2]);
	    if("3".equals(str)) pressKeyCode(key[3]);
	    if("4".equals(str)) pressKeyCode(key[4]);
	    if("5".equals(str)) pressKeyCode(key[5]);
	    if("6".equals(str)) pressKeyCode(key[6]);
	    if("7".equals(str)) pressKeyCode(key[7]);
	    if("8".equals(str)) pressKeyCode(key[8]);
	    if("9".equals(str)) pressKeyCode(key[9]);

	}
	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		setTestName("");
		launchApp("com.android.calculator2", ".Calculator");
		start = System.currentTimeMillis();
		super.setUp();
	}

	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		pressKey("Home");
		end = System.currentTimeMillis();
		long duration = end - start;
		String status = "AppName:"+ appName +" TestName:" + getTestName()
                + " duration: " + String.valueOf(duration) + "ms";
		Log.v(TAG, status);
		super.tearDown();
	}

	public void testMultiply()  {
	  try {
		setTestName("testMultiply");
		//longClickContentDesc("delete");
		findView("android.widget.Button", 0).longClick();
        sleep(1000);
		pressKeyString("7", false);
		pressKeyString("8", false);
		clickContentDesc("multiply", false);
		pressKeyString("7", false);
		pressKeyString("8", false);
		clickContentDesc("equals", false);
		sleep(300);
		assertTextPresent("6084");
	  } catch (UiObjectNotFoundException e){
		  Log.v(TAG, "AppName:"+appName + " TestName:" + getTestName() + " Failed. Because of " +  "Can't find " + e.getMessage());
		  this.failTest("TestName:" + getTestName() + " Failed. Because of " +  "Can't find " + e.getMessage());

	  }
	}

	public void testPlus() throws UiObjectNotFoundException{
		setTestName("testPlus");
		findView("android.widget.Button", 0).longClick();
        sleep(1000);
		pressKeyString("1", false);
		pressKeyString("2", false);
		pressKeyString("3", false);
		clickContentDesc("plus", false);
		pressKeyString("4", false);
		pressKeyString("5", false);
		pressKeyString("6", false);
		clickContentDesc("equals", false);
		sleep(300);
		assertTextPresent("579");
	}

	public void testDivide() throws UiObjectNotFoundException{
		setTestName("testDivide");
		findView("android.widget.Button", 0).longClick();
		sleep(1000);
		pressKeyString("7", false);
		pressKeyString("8", false);
		pressKeyString("8", false);
		clickContentDesc("divide", false);
		pressKeyString("1", false);
		pressKeyString("3", false);
		clickContentDesc("equals", false);
		sleep(300);
		assertTrue("Not enquals, divide failed!", isViewWithTextContains("6153846"));
	}

}
